package org.jeecg.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.DbanQuestion;
import org.jeecg.modules.system.service.IDbanQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = "基础资料问题管理表")
@RestController
@RequestMapping("/dban/question")
public class DbanQuestionController {

    @Autowired
    private IDbanQuestionService iDbanQuestionService;

    @PostMapping(value = "add")
    public Result<?> add(@ModelAttribute DbanQuestion dbanQuestion) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String username = sysUser.getUsername();
        String id = sysUser.getId();
        dbanQuestion.setUserName(username);
        dbanQuestion.setUserId(id);
        boolean boo = iDbanQuestionService.save(dbanQuestion);
        if (boo) {
            return Result.ok("添加成功！");
        } else {
            return Result.ok("添加失败！");
        }
    }


    /**
     * 删除主数据
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<DbanQuestion> delete(@RequestParam(name = "id", required = true) Integer id) {

        Result<DbanQuestion> result = new Result<DbanQuestion>();
        DbanQuestion dbanQuestion = iDbanQuestionService.getById(id);
        if (dbanQuestion == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = iDbanQuestionService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * detail
     */
    @GetMapping(value = "/detail")
    public Result<DbanQuestion> queryPageList(@RequestParam Integer id) {
        DbanQuestion dbanQuestion = iDbanQuestionService.getById(id);
        return Result.ok(dbanQuestion);
    }

    @GetMapping(value = "/list")
    public Result<?> queryPageList(DbanQuestion dbanQuestion,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        QueryWrapper<DbanQuestion> queryWrapper = QueryGenerator.initQueryWrapper(dbanQuestion, req.getParameterMap());
        Page<DbanQuestion> page = new Page<DbanQuestion>(pageNo, pageSize);
        IPage<DbanQuestion> pageList = iDbanQuestionService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    @PostMapping("update")
    public Result<DbanQuestion> updateStatus(DbanQuestion dbanQuestion) {
        Result<DbanQuestion> result = new Result<DbanQuestion>();
        Integer id = dbanQuestion.getId();
        DbanQuestion entity = iDbanQuestionService.getById(id);
        if (entity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean boo = iDbanQuestionService.updateById(dbanQuestion);
            if (boo) {
                result.success("更新成功!");
            } else {
                result.success("更新失败!");
            }


        }
        return result;
    }

}
